//
//  NewsInfoViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/10.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "NewsInfoViewController.h"

@interface NewsInfoViewController ()

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * sourceLabel;
@property (nonatomic, strong)UIImageView * oneImageView;
@property (nonatomic, strong)UIImageView * twoImageView;
@property (nonatomic, strong)UIImageView * threeImageView;
@property (nonatomic, weak)UITextView * textView;


@end

@implementation NewsInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}


- (void)viewWillAppear:(BOOL)animated{
    
//    NSLog(@"====%@",self.ID);
    
    NSString * url = [NSString stringWithFormat:@"index/Common/getArtDetails?id=%@",self.ID];
    
    [[NetworkTool sharedTools] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
//            NSLog(@"===%@",responseObject);
            
            NSDictionary * data = responseObject[@"data"];
            
            self.titleLabel.text = data[@"title"];
            
            // timeStampString 是服务器返回的13位时间戳
            NSString *timeStampString  = data[@"ctime"];
            
            // iOS 生成的时间戳是10位
            NSTimeInterval interval    =[timeStampString doubleValue];
            NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateString       = [formatter stringFromDate: date];
            
            self.timeLabel.text = dateString;
            self.sourceLabel.text = [NSString stringWithFormat:@"来源：%@",data[@"name"]];
//            self.textView.text = data[@"content"];
            NSArray * imageArr = data[@"images"];
            
            if (imageArr.count == 1) {
                
                NSURL * url = [NSURL URLWithString:imageArr.firstObject];
                
                [self.oneImageView sd_setImageWithURL:url];
             
                self.twoImageView.hidden = YES;
                self.threeImageView.hidden = YES;

                UITextView *textView =[[UITextView alloc] init];
                textView.backgroundColor= [UIColor whiteColor];
                textView.text = data[@"content"];
                textView.textColor = [UIColor blackColor];
                textView.editable = NO;
                textView.font = [UIFont systemFontOfSize:13 * SCREENH6];
                
                self.textView = textView;
                
                [self.view addSubview:self.textView];
                
                [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.trailing.mas_equalTo(self.oneImageView);
                    make.bottom.mas_equalTo(self.view);
                    make.top.mas_equalTo(self.oneImageView.mas_bottom).offset(5 * SCREENH6);
                }];
                
            }else{
                
                NSURL * urlOne = [NSURL URLWithString:imageArr.firstObject];
                NSURL * urlTwo = [NSURL URLWithString:imageArr[1]];
                NSURL * urlThree = [NSURL URLWithString:imageArr.lastObject];
                
                [self.oneImageView sd_setImageWithURL:urlOne];
                [self.twoImageView sd_setImageWithURL:urlTwo];
                [self.threeImageView sd_setImageWithURL:urlThree];
                
                UITextView *textView =[[UITextView alloc] init];
                textView.backgroundColor= [UIColor whiteColor];
                textView.text = data[@"content"];
                textView.textColor = [UIColor blackColor];
                textView.editable = NO;
                textView.font = [UIFont systemFontOfSize:13 * SCREENH6];
                
                self.textView = textView;
                
                [self.view addSubview:self.textView];
                
                [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.trailing.mas_equalTo(self.threeImageView);
                    make.bottom.mas_equalTo(self.view);
                    make.top.mas_equalTo(self.threeImageView.mas_bottom).offset(5 * SCREENH6);
                }];

            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (void)setupNav{
    
    self.title = @"详情";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"沙发背后前往牛尔发布发你";
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.text = @"2018年07月18日 15：32";
    timeLabel.textColor = ColorWithRGB(0x808080);
    timeLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.view addSubview:self.timeLabel];

    
    
    UILabel * sourceLabel = [UILabel new];
    
    sourceLabel.text = @"来源：中国人民日报";
    sourceLabel.textColor = ColorWithRGB(0x808080);
    sourceLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.sourceLabel = sourceLabel;
    
    [self.view addSubview:self.sourceLabel];
  
    
    self.oneImageView = [self setupImageView];
    [self.view addSubview:self.oneImageView];
    
    self.twoImageView = [self setupImageView];
    [self.view addSubview:self.twoImageView];
    
    self.threeImageView = [self setupImageView];
    [self.view addSubview:self.threeImageView];
    
    
}


- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel.mas_trailing).offset(20 * SCREENH6);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
    [self.oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-40 * SCREENH6);
        make.height.mas_equalTo(120 * SCREENH6);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(25 *  SCREENH6);
    }];
    
    [self.twoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneImageView);
        make.top.mas_equalTo(self.oneImageView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.threeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoImageView);
        make.top.mas_equalTo(self.twoImageView.mas_bottom).offset(5 * SCREENH6);
    }];
    
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self.threeImageView);
//        make.bottom.mas_equalTo(self.view);
//        make.top.mas_equalTo(self.threeImageView.mas_bottom).offset(5 * SCREENH6);
//    }];
}


- (UIImageView *)setupImageView{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.backgroundColor = [UIColor lightGrayColor];
    
    return imageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
